package middleware

import (
	"log"
	"net/http"
)

type Auth struct {
	Next http.Handler
}

func (a *Auth) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	if a.Next == nil {
		a.Next = http.DefaultServeMux
	}

	// before
	// 处理认证
	auth := request.Header.Get("Authorization")
	if auth == "" {
		// 没有认证信息, 返回错误信息
		http.Error(writer, http.StatusText(http.StatusUnauthorized),
			http.StatusUnauthorized)
		return
	}

	log.Printf("Request incomming: path = %s\n",
		request.URL.Path)

	a.Next.ServeHTTP(writer, request)
	// after
}
